#include "jpg.h"
#include "bmp.h"
#include "touch.h"

int main(int args,const char *argv[])
{
    if(args != 4)
    {
        cout << "请指定要作为拼图的图片以及拼图难度和时间" << endl;
        return 0;
    }
    int n = atoi(argv[2]);
    //int m = atoi(argv[3]);
    //double m = stod(argv[3]);
    double m = atof(argv[3]);

    cout << "n:" << n << endl;
    cout << "m:" << m << endl;

    LCD lcd(n);
    if(strstr(argv[1],".jpg"))
    {
        time_t startTime = time(NULL);
        JPG jpg(argv[1],lcd,n);
        jpg.playman(lcd);
        sleep(2);
        jpg.randomize();
        jpg.playman(lcd);
        TOUCH touch;
        while(1)
        {
            time_t endTime = time(NULL);
            double elapsedSeconds = difftime(endTime, startTime);
            if(elapsedSeconds > m)
            {
                cout << "时间到,游戏失败" << endl;
                return 0;
            }
            int a = touch.XYinfo(n);
            int b = touch.XYinfo(n);
            while(1)
            {
                if(a == b ||(abs(a-b)!=1 && abs(a-b)!=n))
                {
                    b = touch.XYinfo(n);
                }
                else if(a != b && (abs(a-b)==1 || abs(a-b)==n) )
                {
                    cout << "a:" << a << " " << "b:" << b << endl;
                    jpg.move(a,b,lcd);
                    if(jpg.ifwin())
                    {
                        cout << "游戏胜利" << endl;
                        return 0;
                    }
                    break;
                }
            }
        }
    }
    else if(strstr(argv[1],".bmp"))
    {
        time_t startTime = time(NULL);
        BMP bmp(argv[1],lcd,n);
        bmp.playman(lcd);
        sleep(2);
        bmp.randomize();
        bmp.playman(lcd);
        TOUCH touch;
        while(1)
        {
            time_t endTime = time(NULL);
            double elapsedSeconds = difftime(endTime, startTime);
            if(elapsedSeconds > m)
            {
                cout << "时间到,游戏失败" << endl;
                return 0;
            }
            int a = touch.XYinfo(n);
            int b = touch.XYinfo(n);
            while(1)
            {
                if(a == b ||(abs(a-b)!=1 && abs(a-b)!=n))
                {
                    b = touch.XYinfo(n);
                }
                else if(a != b && (abs(a-b)==1 || abs(a-b)==n) )
                {
                    cout << "a:" << a << " " << "b:" << b << endl;
                    bmp.move(a,b,lcd);
                    if(bmp.ifwin())
                    {
                        cout << "游戏胜利" << endl;
                        return 0;
                    }
                    break;
                }
            }
        }
    }
    else
    {
        cout << "不支持bmp和jpg以外的文件格式" << endl;
        return 0;
    }
    return 0;
}